import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:flutter/services.dart';

///错误信息Widget，该Widget替换了flutter默认的红色错误信息界面
///* 提供复制剪切板功能
///* 提供错误类型配置
class DkErrorWidget extends StatelessWidget {
  final ErrorOption errorOption;

  DkErrorWidget(this.errorOption) : super();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: DkAppBar(DKAppBarParams.withBackNoMenu(context,
            titleContent: errorOption?.title ?? "错误")),
        body: getContent(context));
  }

  Widget getContent(BuildContext context) {
    Widget result;
    switch (errorOption?.errorType) {
      /*case ErrorType.initError:
        result = getInitErrorContent();
        break;*/
      case ErrorType.systemError:
        result = getSystemErrorContent(context);
        break;
      case ErrorType.pageError:
        result = getPageErrorContent();
        break;
      case ErrorType.widgetError:
        result = getWidgetErrorContent();
        break;
      default:
        result = getDefaultContent();
    }
    return result;
  }

  Widget getDefaultContent() {
    return Material(
      color: Colors.white,
      child: LogicPage.errorPage(
          errorParam: LogicParam.logicError(
              tip: "未找到错误类型~",
              btnContent: "退出界面",
              listener: () {
                RouteUtil.popOrCloseCurPage();
              })),
    );
  }

  /*Widget getInitErrorContent() {
    return Material(
      color: Colors.white,
      child: LogicPage.errorPage(
          errorParam: LogicParam.logicError(
              imageUrl: errorOption?.imageUrl,
              tip: "系统初始化错误~",
              btnContent: "退出界面",
              listener: () {
                RouteUtil.closeCurrentPage({});
              })),
    );
  }*/

  Widget getSystemErrorContent(BuildContext context) {
    Widget result;
    switch (Env?.value?.type) {
      case EnvType.DEBUG:
      case EnvType.COMMIT_TEST:
      case EnvType.PRE_RELEASE:
        String exception;
        try {
          exception = errorOption?.flutterErrorDetails?.exception.toString();
        } catch (e) {
          exception = 'Error';
        }
        result = SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                margin: EdgeInsets.only(
                  top: 20,
                ),
                child: MaterialButton(
                  onPressed: () {
                    Clipboard.setData(
                      ClipboardData(
                        text: errorOption?.flutterErrorDetails?.toString(),
                      ),
                    );
                    showLongToast("已复制到剪切板");
                  },
                  child: Text(
                    "复制到剪切板",
                    style: TextStyle(color: Colors.white),
                  ),
                  color: Colors.blue,
                ),
              ),
              getHeader("分类信息"),
              getContentItem("context",
                  errorOption?.flutterErrorDetails?.context.toString()),
              getContentItem("silent",
                  errorOption?.flutterErrorDetails?.silent?.toString()),
              getContentItem("exception", exception),
              getContentItem(
                  "stack", errorOption?.flutterErrorDetails?.stack?.toString()),
              getContentItem("library",
                  errorOption?.flutterErrorDetails?.library?.toString()),
              getHeader("整体信息"),
              getContentItem(
                  "all", errorOption?.flutterErrorDetails?.toString()),
            ],
          ),
        );
        break;
      case EnvType.RELEASE:
      default:
        result = Material(
          color: Colors.white,
          child: LogicPage.errorPage(
            errorParam: LogicParam.logicError(
                imageUrl: errorOption?.imageUrl,
                tip: "系统出错啦~",
                btnContent: errorOption?.btnContent,
                listener: () {
                  RouteUtil.closeCurrentPage({});
                }),
          ),
        );
    }
    return result;
  }

  Widget getPageErrorContent() {
    return Material(
      color: errorOption?.bgColor ?? Colors.white,
      child: LogicPage.errorPage(
          errorParam: errorOption?.logicParam ??
              LogicParam.logicError(
                  imageUrl: errorOption?.imageUrl,
                  tip: errorOption?.tip ?? "页面出错啦~",
                  btnContent: errorOption?.btnContent,
                  listener: errorOption?.listener ??
                      () {
                        RouteUtil.closeCurrentPage({});
                      })),
    );
  }

  Widget getWidgetErrorContent() {
    return Material(
      color: errorOption?.bgColor ?? Colors.white,
      child: LogicPage.errorPage(
          errorParam: errorOption?.logicParam ??
              LogicParam.logicError(
                tip: errorOption?.tip ?? "布局出错啦~",
              )),
    );
  }

  final TextStyle headerStyle = TextStyle(fontSize: 18, color: Colors.black);
  final TextStyle titleStyle = TextStyle(fontSize: 16, color: Colors.black);
  final TextStyle contentStyle = TextStyle(fontSize: 16, color: Colors.red);

  Widget getHeader(String header, {double top = 20}) {
    return Padding(
      padding: EdgeInsets.only(top: top),
      child: Text(header, style: headerStyle),
    );
  }

  Widget getContentItem(String title, String content) {
    return Padding(
        padding: EdgeInsets.only(top: 5),
        child: Text.rich(TextSpan(
            text: title + "：\n",
            style: titleStyle,
            children: <TextSpan>[
              TextSpan(style: contentStyle, text: content)
            ])));
  }
}

///错误类型定义，系统错误，界面错误，Widget错误
enum ErrorType { /*initError, */ systemError, pageError, widgetError }

///错误信息配置
///* flutterErrorDetails:系统错误回调的信息
///* title:错误界面的title
///* bgColor:错误界面的背景色
///* tip:错误界面的提示语
///* listener:错误界面的按钮的点击事件
///* logicParam:错误界面的需要显示logic时的配置
///* errorType:错误界面的类型
class ErrorOption {
  FlutterErrorDetails flutterErrorDetails;
  String title;
  Color bgColor;
  String imageUrl;
  String tip;
  String btnContent;
  LogicBtnClickListener listener;
  LogicParam logicParam;
  ErrorType errorType;

  ErrorOption.systemError(this.flutterErrorDetails,
      {this.imageUrl, this.btnContent = "退出界面"}) {
    errorType = ErrorType.systemError;
    title = "系统错误";
  }

  /*ErrorOption.initError({this.imageUrl}) {
    errorType = ErrorType.initError;
  }*/

  ErrorOption.pageError({
    this.title,
    this.bgColor = Colors.white,
    this.imageUrl,
    this.tip,
    this.btnContent = "退出界面",
    this.listener,
    this.logicParam,
  }) {
    errorType = ErrorType.pageError;
  }

  ErrorOption.widgetError({
    this.title,
    this.bgColor,
    this.tip,
    this.logicParam,
  }) {
    errorType = ErrorType.widgetError;
  }
}
